package com.ln.asset.core.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ln.asset.common.result.R;
import com.ln.asset.core.pojo.Car;
import com.ln.asset.core.pojo.CarHistory;
import com.ln.asset.core.service.CarHistoryService;
import com.ln.asset.core.service.CarService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sishengyang
 * @since 2022-05-02
 */
@Api(tags = "车辆管理")
@RestController
@RequestMapping("/admin/core/car")
@Slf4j
public class CarController {

    @Resource
    private CarService carService;

    @Resource
    private CarHistoryService carHistoryService;

    @ApiOperation("全查")
    @GetMapping("/lists")
    public R list(){
        List<Car> list = carService.list();
        return R.ok().data("lists",list);
    }

    @ApiOperation("获取车辆列表")
    @GetMapping("/list/{page}/{limit}")
    public R listpage(
            @ApiParam(value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(value = "每页记录数", required = true)
            @PathVariable Long limit){
        Page<Car> pageParam = new Page<>(page, limit);
        IPage<Car> list = carService.page(pageParam);
        System.out.println("+++"+list);
        return R.ok().data("list",list);
    }


    @ApiOperation("删除车辆信息")
    @DeleteMapping("/remove/{id}")
    public R removeId(
            @ApiParam(value = "车辆id",example = "100",required = true)
            @PathVariable Integer id){

        boolean result = carService.removeById(id);
        if (result){
            return R.ok().message("删除成功");
        }else{
            return R.error().message("删除失败");
        }
    }

    @ApiOperation("新增车辆")
    @PostMapping("/save")
    public R save(
            @ApiParam(value = "新增车辆",required = true)
            @RequestBody Car car){
        car.setIsDeleted(0);
        boolean result = carService.save(car);
        if (result){
            return R.ok().message("保存成功");
        }else {
            return R.error().message("保存失败");
        }
    }

    @ApiOperation("根据id查询车辆")
    @GetMapping("/get/{id}")
    public R getById(
            @ApiParam(value = "车辆id",required = true,example = "1")
            @PathVariable Integer id){

        Car car = carService.getById(id);
        if (car !=null){
            return R.ok().data("car",car);
        }else {
            return R.error().message("车辆获取失败");
        }
    }

    @ApiOperation("更新积分列表")
    @PutMapping("/update")
    public R updateById(
        @ApiParam(value = "车辆对象",required = true)
        @RequestBody Car car){

        boolean result = carService.updateById(car);
        if(result){
            return R.ok().message("更新成功");
        }else {
            return R.error().message("更新失败");
        }
    }

    @ApiOperation("审批车辆状态")
    @PutMapping("/CarUpdateById")
    public R CarUpdateById(
            @RequestBody Car car){
        carService.CarUpdateById(car);
        return R.ok().message("审批完成");
    }

}

